package tk.flexwork.components.simpleeditor
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Button;
	import mx.core.UIComponent;
	import mx.managers.CursorManager;
	
	import spark.components.Group;
	

	public class Shell extends Group
	{
		public function Shell()
		{
		}
		
		public var borderVisible:Boolean=false;
		
		[Bindable]
		public var desc:ShellDesc=new ShellDesc();
		
		private var _content:UIComponent;

		public function get content():UIComponent
		{
			return _content;
		}

		public function set content(value:UIComponent):void
		{
			_content = value;
			addElement(value);
			invalidateDisplayList();
		}
		
		private var leftTop:int=1;
		
		private var leftToRight:int=2;
		
		private var rightTop:int=3;
		
		private var leftToBottom:int=4;
		
		private var leftBottom:int=5;
		
		private var rightToBottom:int=6;
		
		private var rightBottom:int=7;
		
		private var leftButtomToRightBottom:int=8;
		
		
		private var _btnLeftTop:Button;
		
		private var _btnLeftToRight:Button;
		
		private var _btnRightTop:Button;
		
		private var _btnLeftToBottom:Button;
		
		private var _btnLeftBottom:Button;
		
		private var _btnRightToBottom:Button;
		
		private var _btnRightBottom:Button;
		
		private var _btnLeftBottomToRightBottom:Button;
		
		private var _btnLeftTopVisible:Boolean=true;

		public function get btnLeftTopVisible():Boolean
		{
			return _btnLeftTopVisible;
		}

		public function set btnLeftTopVisible(value:Boolean):void
		{
			_btnLeftTopVisible = value;
			invalidateDisplayList();
		}

		
		private var _btnLeftToRightVisible:Boolean=true;

		public function get btnLeftToRightVisible():Boolean
		{
			return _btnLeftToRightVisible;
		}

		public function set btnLeftToRightVisible(value:Boolean):void
		{
			_btnLeftToRightVisible = value;
			invalidateDisplayList();
		}

		
		private var _btnRightTopVisible:Boolean=true;

		public function get btnRightTopVisible():Boolean
		{
			return _btnRightTopVisible;
		}

		public function set btnRightTopVisible(value:Boolean):void
		{
			_btnRightTopVisible = value;
			invalidateDisplayList();
		}

		
		private var _btnLeftToBottomVisible:Boolean=true;

		public function get btnLeftToBottomVisible():Boolean
		{
			return _btnLeftToBottomVisible;
		}

		public function set btnLeftToBottomVisible(value:Boolean):void
		{
			_btnLeftToBottomVisible = value;
			invalidateDisplayList();
		}

		
		private var _btnLeftBottomVisible:Boolean=true;

		public function get btnLeftBottomVisible():Boolean
		{
			return _btnLeftBottomVisible;
		}

		public function set btnLeftBottomVisible(value:Boolean):void
		{
			_btnLeftBottomVisible = value;
			invalidateDisplayList();
		}

		
		private var _btnRightToBottomVisible:Boolean=true;

		public function get btnRightToBottomVisible():Boolean
		{
			return _btnRightToBottomVisible;
		}

		public function set btnRightToBottomVisible(value:Boolean):void
		{
			_btnRightToBottomVisible = value;
			invalidateDisplayList();
		}

		
		private var _btnRightBottomVisible:Boolean=true;

		public function get btnRightBottomVisible():Boolean
		{
			return _btnRightBottomVisible;
		}

		public function set btnRightBottomVisible(value:Boolean):void
		{
			_btnRightBottomVisible = value;
			invalidateDisplayList();
		}

		
		private var _btnLeftButtomToRightBottomVisible:Boolean=true;

		public function get btnLeftButtomToRightBottomVisible():Boolean
		{
			return _btnLeftButtomToRightBottomVisible;
		}

		public function set btnLeftButtomToRightBottomVisible(value:Boolean):void
		{
			_btnLeftButtomToRightBottomVisible = value;
			invalidateDisplayList();
		}
		

		private var _contentPadding:int=10;

		public function get contentPadding():int
		{
			return _contentPadding;
		}

		public function set contentPadding(value:int):void
		{
			_contentPadding = value;
			invalidateDisplayList();
		}
		
		
		private var _btnWidth:int=8;

		public function get btnWidth():int
		{
			return _btnWidth;
		}

		public function set btnWidth(value:int):void
		{
			_btnWidth = value;
		}

		//cursor
		[Embed(source="assets/cursorEastWest.jpg")]
		private var cursorEastWest:Class;
		
		[Embed(source="assets/cursorSouthNorth.jpg")]
		private var cursorSouthNorth:Class;
		
		[Embed(source="assets/cursorWestNorth.jpg")]
		private var cursorWestNorth:Class;
		
		[Embed(source="assets/cursorNorthEast.jpg")]
		private var cursorNorthEast:Class;
		
		private var previouseCursorId:Number=Number.NaN;
		
		private var resizeDirection:int=0;
		
		private var mouseOffetSet:Point=new Point(0,0);
		
		private var previouseMousePosition:Point=new Point(0,0);
		
		override protected function createChildren():void{
			if(!_btnLeftTop){
				_btnLeftTop=createButton();
				addElement(_btnLeftTop);
			}
			
			if(!_btnLeftToRight){
				_btnLeftToRight=createButton();
				addElement(_btnLeftToRight);
			}
			
			if(!_btnLeftToBottom){
				_btnLeftToBottom=createButton();
				addElement(_btnLeftToBottom);
			}
			
			if(!_btnRightTop){
				_btnRightTop=createButton();
				addElement(_btnRightTop);
			}
			
			if(!_btnRightToBottom){
				_btnRightToBottom=createButton();
				addElement(_btnRightToBottom);
			}
			
			if(!_btnRightBottom){
				_btnRightBottom=createButton();
				addElement(_btnRightBottom);
			}
			
			if(!_btnLeftBottom){
				_btnLeftBottom=createButton();
				addElement(_btnLeftBottom);
			}
			
			if(!_btnLeftBottomToRightBottom){
				_btnLeftBottomToRightBottom=createButton();
				addElement(_btnLeftBottomToRightBottom);
			}
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			super.createChildren();
		}
		
		private var readyToMove:Boolean=false;
		private function onMouseDown(event:MouseEvent):void{
			switch(event.target){
				case _btnRightBottom:{
					previouseCursorId=CursorManager.setCursor(cursorWestNorth);
					resizeDirection=5;
					break;
				}
				case _btnLeftTop:{
					previouseCursorId=CursorManager.setCursor(cursorWestNorth);
					resizeDirection=1;
					break;
				}
				case _btnLeftBottomToRightBottom:{
					previouseCursorId=CursorManager.setCursor(cursorSouthNorth);
					resizeDirection=6;
					break;
				}
				case _btnLeftToRight:{
					previouseCursorId=CursorManager.setCursor(cursorSouthNorth);
					resizeDirection=2;
					break;
				}
				case _btnLeftBottom:{
					previouseCursorId=CursorManager.setCursor(cursorNorthEast);
					resizeDirection=7;
					break;
				}
				case _btnRightTop:{
					previouseCursorId=CursorManager.setCursor(cursorNorthEast);
					resizeDirection=3;
					break;
				}
				case _btnLeftToBottom:{
					previouseCursorId=CursorManager.setCursor(cursorEastWest);
					resizeDirection=8;
					break;
				}
				case _btnRightToBottom:{
					previouseCursorId=CursorManager.setCursor(cursorEastWest);
					resizeDirection=4;
					break;
				}case content:{
				}case this:{
					readyToMove=true;
				}
					
			}
			mouseOffetSet.x = event.stageX - x;
			mouseOffetSet.y = event.stageY - y;
			previouseMousePosition.x=event.stageX;
			previouseMousePosition.y=event.stageY;
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		
		
		private function onMouseUp(event:MouseEvent):void{
			readyToMove=false;
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			CursorManager.removeCursor(previouseCursorId);
		}
		
		private function onMouseMove(event:MouseEvent):void{
			if(!readyToMove){
				var horizontalDiscrepancy:int=event.stageX-previouseMousePosition.x;
				var verticalDiscrepancy:int=event.stageY-previouseMousePosition.y;
				previouseMousePosition.x=event.stageX;
				previouseMousePosition.y=event.stageY;
				switch(resizeDirection){
					case 1:{
						x+=horizontalDiscrepancy
						y+=verticalDiscrepancy;
						width-=horizontalDiscrepancy;
						height-=verticalDiscrepancy;
						break;
					}
					case 2:{
						y+=verticalDiscrepancy;
						height-=verticalDiscrepancy;
						break;
					}
					case 3:{
						y+=verticalDiscrepancy;
						width+=horizontalDiscrepancy;
						height-=verticalDiscrepancy;
						break;
					}
					case 4:{
						width+=horizontalDiscrepancy;
						break;
					}
					case 5:{
						width+=horizontalDiscrepancy;
						height+=verticalDiscrepancy;
						break;
					}
					case 6:{
						height+=verticalDiscrepancy;
						break;
					}
					case 7:{
						x+=horizontalDiscrepancy;
						width-=horizontalDiscrepancy;
						height+=verticalDiscrepancy
						break;
					}
					case 8:{
						x+=horizontalDiscrepancy
						width-=horizontalDiscrepancy;
						break;
					}
						
				}
			}else{
				x = event.stageX - mouseOffetSet.x;
				y = event.stageY - mouseOffetSet.y;
			}
		}
		
		private function createButton():Button{
			var newButton:Button=new Button();
			newButton.width=btnWidth;
			newButton.height=btnWidth;
			newButton.alpha=0.5;
			return newButton;
		}
		
		private function resizeCoetent(event:MouseEvent):void{
			var point:Point=localToGlobal(new Point(0,0));
			switch(event.target){
				case _btnLeftTop:{
					
					break;
				}
			
			}
			
		}
		
		override public function set width(value:Number):void{
			var minWidth:int=btnWidth*2;
			if(value<minWidth){
				value=minWidth;
			}
			if(parent&&(value>parent.width)){
				x=0;
			}
			super.width=value;
		}
		
		override public function set height(value:Number):void{
			var minHeight:int=btnWidth*2;
			if(value<minHeight){
				value=minHeight;
			}
			if(parent&&(value>parent.height)){
				y=0;
			}
			super.height=value;
		}
		
		override public function set x(value:Number):void{
			if(value<0){value=0}
			if(parent&&(value>(parent.width-width))){
				value=parent.width-width
			}
			super.x=value;
		}
		
		override public function set y(value:Number):void{
			if(value<0){value=0}
			if(parent&&(value>(parent.height-height))){
				value=parent.height-height
			}
			super.y=value;
		}
		
		override public function invalidateProperties():void{
			super.invalidateProperties();
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			if(content){
				if(content.rotation==90||content.rotation==-90){
					content.width=height-contentPadding*2;
					content.height=width-contentPadding*2;
				}else{
					content.width=width-contentPadding*2;
					content.height=height-contentPadding*2;
				}
				content.top=contentPadding;
				content.left=contentPadding;
			}
			
			if(_btnLeftTop){
				_btnLeftTop.left=-btnWidth/2;
				_btnLeftTop.top=-btnWidth/2;
				_btnLeftTop.visible=btnLeftTopVisible;
			}
			
			if(_btnLeftToBottom){
				_btnLeftToBottom.left=-btnWidth/2;
				_btnLeftToBottom.top=height/2-btnWidth/2;
				_btnLeftToBottom.visible=btnLeftToBottomVisible;
			}
			if(_btnLeftBottom){
				_btnLeftBottom.left=-btnWidth/2;
				_btnLeftBottom.top=height-btnWidth/2;
				_btnLeftBottom.visible=btnLeftBottomVisible;
			}
			
			if(_btnLeftToRight){
				_btnLeftToRight.left=width/2-btnWidth/2;
				_btnLeftToRight.top=-btnWidth/2;
				_btnLeftToRight.visible=btnLeftToRightVisible;
			}
			
			if(_btnRightBottom){
				_btnRightBottom.left=width-btnWidth/2;
				_btnRightBottom.top=height-btnWidth/2;
				_btnRightBottom.visible=btnRightBottomVisible;
			}
			
			if(_btnRightToBottom){
				_btnRightToBottom.left=width-btnWidth/2;
				_btnRightToBottom.top=height/2-btnWidth/2;
				_btnRightToBottom.visible=btnRightToBottomVisible;
			}
			
			if(_btnRightTop){
				_btnRightTop.left=width-btnWidth/2;
				_btnRightTop.top=-btnWidth/2;
				_btnRightTop.visible=btnLeftToRightVisible;
			}
			
			if(_btnLeftBottomToRightBottom){
				_btnLeftBottomToRightBottom.left=width/2-btnWidth/2;
				_btnLeftBottomToRightBottom.top=height-btnWidth/2;
				_btnLeftBottomToRightBottom.visible=btnLeftButtomToRightBottomVisible;
			}
			
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			graphics.clear();
			if(borderVisible){
				graphics.lineStyle(1,0xAAAAA);
				graphics.lineTo(width,0);
				graphics.lineTo(width,height);
				graphics.lineTo(0,height);
				graphics.lineTo(0,0);
				//file transparent background color,if no background color fill the padding area can not active mousedown event
				graphics.beginFill(0xAAAAA,0);
				graphics.drawRect(0,0,width,height);
			}
		}
		
		override public function set rotation(value:Number):void{
			value=value%360;
			if(value<0){
				value+=360;
			}
			if(content){
				content.rotation=value;
			}
//			rotation=value;
		}
		
		public function updateShell(value:ShellDesc):void{
			if(this.desc&&value){
//				if(value.x)
			}
		}

	}
}